using Microsoft.Phone.Controls.Maps.AutomationPeers;
using Microsoft.Phone.Controls.Maps.Core;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Collections.Specialized;
using System.Text;
using System.Windows;
using System.Windows.Automation.Peers;
namespace Microsoft.Phone.Controls.Maps.Overlays
{
	[TemplatePart(Name = "CopyrightContainer", Type = typeof(ShadowText))]
	public class Copyright : Overlay
	{
		internal const string CopyrightContainerElementName = "CopyrightContainer";
		private const int maxLine = 60;
		private const string spacesBetween = "   ";
		private const char nonBreakingSpace = '\u00a0';
		private ObservableCollection<AttributionInfo> attributions;
		private ShadowText container;
		private ModeBackground setForBackground;
		public ObservableCollection<AttributionInfo> Attributions
		{
			get
			{
				return this.attributions;
			}
			internal set
			{
				if (this.attributions != null)
				{
					this.attributions.remove_CollectionChanged(new NotifyCollectionChangedEventHandler(this.Attributions_CollectionChanged));
				}
				if (value != null)
				{
					this.attributions = value;
					this.attributions.add_CollectionChanged(new NotifyCollectionChangedEventHandler(this.Attributions_CollectionChanged));
				}
			}
		}
		internal ModeBackground SetForBackground
		{
			get
			{
				return this.setForBackground;
			}
			set
			{
				this.setForBackground = value;
				if (this.setForBackground == ModeBackground.Light)
				{
					this.container.SetForegroundColorsForLightBackground();
					return;
				}
				this.container.SetForegroundColorsForDarkBackground();
			}
		}
		private ShadowText Container
		{
			get
			{
				return this.container;
			}
			set
			{
				if (value != null)
				{
					this.SetContainer(value);
				}
			}
		}
		public Copyright()
		{
			base.set_DefaultStyleKey(typeof(Copyright));
			this.container = new ShadowText();
			this.Attributions = new ObservableCollection<AttributionInfo>();
		}
		private void SetContainer(ShadowText newContainer)
		{
			if (this.container != null)
			{
				newContainer.Text = this.container.Text;
			}
			this.container = newContainer;
		}
		private void Attributions_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
		{
			this.OnAttributionsChanged(e.get_OldItems(), e.get_NewItems());
		}
		private void OnAttributionsChanged(IList oldItems, IList newItems)
		{
			int num = 0;
			StringBuilder stringBuilder = new StringBuilder();
			using (IEnumerator<AttributionInfo> enumerator = this.Attributions.GetEnumerator())
			{
				while (enumerator.MoveNext())
				{
					AttributionInfo current = enumerator.get_Current();
					if (num > 0)
					{
						if (num + current.Text.get_Length() > 60)
						{
							stringBuilder.AppendLine();
							num = 0;
						}
						else
						{
							stringBuilder.Append("   ");
							num += "   ".get_Length();
						}
					}
					Copyright.AddNonBreakingString(stringBuilder, current.Text);
					num += current.Text.get_Length();
				}
			}
			this.container.Text = stringBuilder.ToString();
		}
		private static void AddNonBreakingString(StringBuilder sb, string s)
		{
			for (int i = 0; i < s.get_Length(); i++)
			{
				char c = s.get_Chars(i);
				if (' ' == c)
				{
					c = '\u00a0';
				}
				sb.Append(c);
			}
		}
		protected override AutomationPeer OnCreateAutomationPeer()
		{
			return new BaseAutomationPeer(this, "Copyright");
		}
		public override void OnApplyTemplate()
		{
			base.OnApplyTemplate();
			this.Container = (base.GetTemplateChild("CopyrightContainer") as ShadowText);
			base.FireTemplateApplied();
		}
	}
}
